<!DOCTYPE html>
<html>
    <head>
        <!-- 页面meta -->
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>传智健康</title>
        <meta name="description" content="传智健康">
        <meta name="keywords" content="传智健康">
        <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
        <!-- 引入样式 -->
        <link rel="stylesheet" href="../plugins/elementui/index.css">
        <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
        <link rel="stylesheet" href="../css/style.css">
    </head>
    <body class="hold-transition">
        <div id="app">

            <!-- 页面头部 -->
            <div class="content-header">
                <h1>工作台<small>菜单管理</small></h1>
                <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
                    <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
                    <el-breadcrumb-item>工作台</el-breadcrumb-item>
                    <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
                </el-breadcrumb>
            </div>

            <div class="app-container">
                <div class="box">

                    <!-- 搜索框 -->
                    <div class="filter-container">
                        <!--<el-input placeholder="项目编码/项目名称" v-model="pagination.queryString" style="width: 200px;" class="filter-item"></el-input>
                        <el-button @click="_findPage()" class="dalfBut">查询</el-button>-->
                        <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
                    </div>

                    <!-- 页面显示的数据 -->
                    <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                        <el-table-column type="index" align="center" label="序号"></el-table-column>
                        <el-table-column prop="name" label="菜单名称" align="center"></el-table-column>
                        <el-table-column prop="linkUrl" label="菜单请求路径" align="center"></el-table-column>
                        <el-table-column prop="description" label="菜单描述" align="center"></el-table-column>
                        <el-table-column prop="priority" label="菜单优先级" align="center"></el-table-column>
                        <el-table-column label="操作" align="center">
                            <template slot-scope="scope">
                                <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                                <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>

                    <!-- 分页栏目的封装 -->
                    <div class="pagination-container">
                        <el-pagination
                            class="pagiantion"
                            @current-change="handleCurrentChange"
                            :current-page="pagination.currentPage"
                            :page-size="pagination.pageSize"
                            layout="total, prev, pager, next, jumper"
                            :total="pagination.total">
                        </el-pagination>
                    </div>

                    <!-- 新增标签弹层 -->
                    <div class="add-form">
                        <el-dialog title="新增菜单" :visible.sync="dialogFormVisible">
                            <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="菜单名称" prop="name">
                                            <el-input v-model="formData.name"/>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="请求路径" prop="linkUrl">
                                            <el-input v-model="formData.linkUrl"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="24">
                                        <el-form-item label="路由路径">
                                            <el-input v-model="formData.path" type="textarea"></el-input>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="24">
                                        <el-form-item label="父菜单id">
                                            <el-input v-model="formData.parentMenuId" type="textarea"></el-input>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="24">
                                        <el-form-item label="菜单描述">
                                            <el-input v-model="formData.description" type="textarea"></el-input>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                            </el-form>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click="dialogFormVisible = false">取消</el-button>
                                <el-button type="primary" @click="handleAdd()">确定</el-button>
                            </div>
                        </el-dialog>
                    </div>

                    <!-- 编辑标签弹层 -->
                    <div class="add-form">
                        <el-dialog title="编辑菜单" :visible.sync="dialogFormVisible4Edit">
                            <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="菜单名称" prop="name">
                                            <el-input v-model="formData.name"/>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="请求路径" prop="linkUrl">
                                            <el-input v-model="formData.linkUrl"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="24">
                                        <el-form-item label="路由路径">
                                            <el-input v-model="formData.path" type="textarea"></el-input>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="24">
                                        <el-form-item label="父菜单id">
                                            <el-input v-model="formData.parentMenuId" type="textarea"></el-input>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="24">
                                        <el-form-item label="菜单描述">
                                            <el-input v-model="formData.description" type="textarea"></el-input>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                            </el-form>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                                <el-button type="primary" @click="handleEdit()">确定</el-button>
                            </div>
                        </el-dialog>
                    </div>
                </div>
            </div>
        </div>
    </body>
    <!-- 引入组件库 -->
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <script>
        var vue = new Vue({
            el: '#app',
            data:{
                pagination: {//分页相关模型数据
					  currentPage: 1,//当前页码
					  pageSize:10,//每页显示的记录数
					  total:0,//总记录数
					  queryString:null//查询条件
				},
				dataList: [],//当前页要展示的分页列表数据
                formData: {},//表单数据
                dialogFormVisible: false,//增加表单是否可见
                dialogFormVisible4Edit:false,//编辑表单是否可见
                rules: {//校验规则
                    name: [{ required: true, message: '菜单名称为必填项', trigger: 'blur' }],
                    linkUrl: [{ required: true, message: '请求路径为必填项', trigger: 'blur' }]
                },
                backendURL:"http://localhost:82"
            },
            //钩子函数，VUE对象初始化完成后自动执行
            created() {
                //VUE的生命周期，自动执行查询所有的方法。
                this.findPage();
            },
            methods: {
                //编辑
                handleEdit() {
                    //校验数据，如果失败，不提交
                    this.$refs['dataEditForm'].validate((valid)=>{
                       if (!valid) {
                           return false;

                       }else {

                           // 设置跨域访问，withCredentials必须为true
                           axios.defaults.withCredentials = true;
                           //校验成功,发起请求
                           axios.post(this.backendURL+"/menu/edit.do",this.formData).then((res)=>{
                               //查询所有
                               this.findPage();

                               //消息提示
                               if (res.data.flag){
                                    this.$message({
                                        message:res.data.message,
                                        type:'success'
                                    });
                               }else {
                                   this.$message({
                                       message:res.data.message,
                                       type:'error'
                                   });
                               }
                               //关闭弹框
                               this.dialogFormVisible4Edit = false;

                           }).catch((err)=>{
                               this.showErrorMessage(err);
                           });

                       }

                    });

                },

                //添加
                handleAdd () {
                    //this.$refs['dataAddForm'] 获取表单数据  validate((valid)=>{}) 校验表单
                    this.$refs['dataAddForm'].validate((valid)=>{
                        if (!valid) {
                            return false;
                        }

                        // 设置跨域访问，withCredentials必须为true
                        axios.defaults.withCredentials = true;
                        //如果成功，发送一个请求  this.formData表单的数据 提交表单的数据给后端  （axios）发起请求
                        axios.post(this.backendURL+"/menu/save.do" , this.formData).then((res)=>{
                            //查询所有
                            this.findPage();
                            //弹框提示
                            console.log(res);
                            if (res.data.flag){
                                this.$message({
                                    message: res.data.message,
                                    type: 'success'
                                });
                            } else {
                                this.$message({
                                    message: res.data.message,
                                    type: 'error'
                                });
                            }
                            //关闭弹框
                            this.dialogFormVisible = false;
                        }).catch((err)=>{
                            this.showErrorMessage(err);
                        });

                    })


                },

                //如果有条件，就查询这个方法
                _findPage(){
                    //如果有条件。就默认查第一页
                  if (this.pagination.queryString != null && this.pagination.queryString.length>0){
                      this.handleCurrentChange(1);
                  }
                  this.findPage();
                },

                //分页查询
                findPage() {
                    //封装参数
                     var param = {
                        currentPage:this.pagination.currentPage,
                        pageSize:this.pagination.pageSize,
                        queryString:this.pagination.queryString
                    }

                    // 设置跨域访问，withCredentials必须为true
                    axios.defaults.withCredentials = true;
                    //发送分页查询的请求
                    axios.post(this.backendURL + "/menu/findPage.do" , param).then((res)=>{
                        console.log(res);
                        //请求完成之后。用表格表格去展示数据，分页展示效果
                        this.dataList = res.data.rows;
                        //把查询出来的总条数给分页组件中
                        this.pagination.total = res.data.total;

                    }).catch((err)=>{
                        this.showErrorMessage(err);
                    });

                },

                // 重置表单
                resetForm(dataForm) {
                    //1.清除表单的数据
                    this.formData = {};
                    //2.清除表单的校验错误信息   this.$refs['dataAddForm']  获取到当前的表单对象
                    if (this.$refs[dataForm] != null){
                        this.$refs[dataForm].resetFields();
                    }
                },
                // 弹出添加窗口
                handleCreate() {
                    //1.弹出框
                    this.dialogFormVisible = true;
                    this.resetForm('dataAddForm');
                },
                // 弹出编辑窗口
                handleUpdate(row) {
                    /*
                        步骤：
                            1.获取ID
                            2.弹出弹框
                            3.清空表单
                            4.根据ID查询检查项，发起请求，然后在页面中显示检查项数据
                     */
                    var id = row.id;

                    this.dialogFormVisible4Edit = true;

                    this.resetForm('dataEditForm');

                    // 设置跨域访问，withCredentials必须为true
                    axios.defaults.withCredentials = true;
                    axios.get(this.backendURL+"/menu/findById.do?id="+id).then((res)=>{
                        //把后端传递过来的data绑定到formData中
                        this.formData = res.data.data;
                    }).catch((err)=>{
                        this.showErrorMessage(err);
                    });

                },

                //切换页码
                handleCurrentChange(currentPage) {
                    this.pagination.currentPage = currentPage;
                    this.findPage();
                },
                // 删除
                handleDelete(row) {
                    this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        // 设置跨域访问，withCredentials必须为true
                        axios.defaults.withCredentials = true;
                        //当点击确认删除时。发起请求
                        axios.get(this.backendURL+"/menu/delById.do?id="+row.id).then((res)=>{
                            if (res.data.flag){
                                this.$message({
                                    type: 'success',
                                    message: res.data.message
                                });
                            }else {
                                this.$message({
                                    type: 'error',
                                    message: res.data.message
                                });
                            }

                            //删除成功，查询所有
                            this.handleCurrentChange(1);

                        }).catch((err)=>{
                            this.showErrorMessage(err);
                        });

                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消删除'
                        });
                    });

                },
                showErrorMessage(err){
                    if (err == 'Error: Request failed with status code 403'){
                        this.$message({
                            type: 'error',
                            message: '权限不足！！'
                        });
                    }else {
                        this.$message({
                            type: 'info',
                            message: '错误，请联系管理员'
                        });
                    }
                }
                
            }
        })
    </script>
</html>